//
//  CXTabBar.m
//  weico
//
//  Created by c_xie on 15-4-9.
//  Copyright (c) 2015年 c_xie. All rights reserved.
//

#import "CXTabBar.h"
#import "UIView+frame.h"

@implementation CXTabBar

- (void)awakeFromNib
{
    [super awakeFromNib];
    // 渲染颜色
    self.tintColor = [UIColor orangeColor];
    // 添加按钮
    self.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    UIButton *btn = [[UIButton alloc] init];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateSelected];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(compose) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];

}

- (void)compose
{
    NSLog(@"%s",__func__);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 设置子控件frame
    CGFloat tabbarW = self.bounds.size.width / 5;
    CGFloat tabbarY = 0;
    CGFloat tabbarH = self.bounds.size.height;
    int index = 0;
    for (UIView *iv in self.subviews) {
        if ([iv isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (index == 2) {
                index ++;
            }
            CGFloat tabbarX = index * tabbarW;
            index ++;
            iv.frame = CGRectMake(tabbarX, tabbarY, tabbarW, tabbarH);
        }
        else if ([iv isKindOfClass:[UIButton class]]) {
            iv.centerX = self.size.width *0.5;
            iv.centerY = self.size.height *0.5;
            iv.width = tabbarW;
            iv.height = tabbarH;
        }
    }

}

@end
